/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons;

import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;

import org.dctmcommons.impl.SessionHelper;
import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

/**
 * Integration tests for {@link SessionHelper}.
 */
public class SessionHelperIT {

	/** Serial id. */
	private static final long serialVersionUID = 5920496640879014635L;
		
	/**
	 * Tests the factory method.
	 */
	@Test
	public void testGetInstance() {
		System.out.println(">>> testGetInstance");
		assertThat("Factory method returned null",
				   SessionHelper.getInstance(),
				   is(notNullValue()));
	}
	
	/**
	 * Tests creation of session manager and session.
	 **/
	@Test
	public void testCreateSession() throws DfException {
		System.out.println(">>> testCreateSession..");

		// get session manager
		final DctmCommonsTestContext ctx = DctmCommonsTestUtil.getContext();
		final ISessionHelper sessionHelper = SessionHelper.getInstance();
		final IDfSessionManager manager = sessionHelper.createSessionManager(
												ctx.getRepository(),
												ctx.getUsername(),
												ctx.getPassword(),
												ctx.getDomain());
		
		assertThat("Session manager is null", manager, is(notNullValue()));
		final IDfLoginInfo li = manager.getIdentity(ctx.getRepository());
		assertThat("Identity is null", li, is(notNullValue()));
		assertThat("Incorrect username", li.getUser(), is(ctx.getUsername()));

		// get session
		final IDfSession session = manager.getSession(ctx.getRepository());
		assertThat("Session is null", session, is(notNullValue()));
		assertThat("Invalid repository name",
				   session.getDocbaseName(),
				   is(ctx.getRepository()));

		sessionHelper.releaseSession(session);
	}
	
	/**
	 * Tests using a session.
	 */
	@Test
	public void useSessionTest() throws Exception {
		System.out.println(">>> useSessionTest..");
		
		final DctmCommonsTestContext ctx = DctmCommonsTestUtil.getContext();
		final ISessionHelper sessionHelper = SessionHelper.getInstance();
		final IDfSessionManager manager = sessionHelper.createSessionManager(
											ctx.getRepository(),
											ctx.getUsername(),
											ctx.getPassword(),
											ctx.getDomain());
		sessionHelper.useSession(manager,
								 ctx.getRepository(),
								 false,
								 new ISessionCallback() {
			@Override
			public void useSession(final IDfSession session)
				throws DfException {
				assertThat("Session is null", session, is(notNullValue()));
				assertThat("Invalid repository name",
						   session.getDocbaseName(),
						   is(ctx.getRepository()));
			}
		});
	}
	
}
